package com.dka.assistants.model.enums;

import lombok.Getter;

import java.util.Arrays;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 用户角色枚举
 */
@Getter
public enum UserRoleEnum {

    USER("用户", "mapper"),
    ADMIN("管理员", "admin"),
    BAN("被封号", "ban");

    private final String text;

    private final String value;

    UserRoleEnum(String text, String value) {
        this.text = text;
        this.value = value;
    }

    /**
     * 根据给定的名称字符串来查找对应的枚举值，允许你通过名称来获取枚举常量
     */
    private static final Map<String, UserRoleEnum> CODE_MAP =
            Arrays.stream(UserRoleEnum.values())
                    .collect(Collectors.toMap(UserRoleEnum::getValue, Function.identity()));

    //得到枚举对象
    public static UserRoleEnum get(String value) {
        return CODE_MAP.get(value);
    }

    //得到业务需要的枚举值
    public static String parseCode(String value) {
        return Optional.ofNullable(UserRoleEnum.get(value)).map(UserRoleEnum::getValue).orElse(null);
    }
}